package com.winning.sjzc.xmgl.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winning.sjzc.common.utils.JSONUtils;
import com.winning.sjzc.common.utils.ResponseEntity;
import com.winning.sjzc.xmgl.entity.XmglEntity;
import com.winning.sjzc.xmgl.service.XmglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: jinx
 * @Description: 项目管理Controller
 * @Date: 2019-04-30
 **/
@RestController
@RequestMapping("/xmgl")
public class XmglController {

    @Autowired
    private XmglService xmglService;

    /**
     * 查询项目管理表数据
     *
     * @return String
     */
    @RequestMapping(value = "/queryXmglTable", method = RequestMethod.POST)
    public String queryXmglTableHandler(@RequestParam String searchVal, @RequestParam int pageNumber, @RequestParam int pageSize) {
        int startNum = (pageNumber-1)*pageSize;
        int endNum = pageNumber*pageSize;
        List<XmglEntity> xmglTableDataEntity = xmglService.queryXmglTableService(searchVal, startNum, endNum);
        if(xmglTableDataEntity.size() > 0){
            JSONObject res = new JSONObject();
            res.put("total", xmglTableDataEntity.size());
            res.put("rows", JSONUtils.toJSONArray(xmglTableDataEntity));
            return res.toString();
        }
        return null;
    }

    /**
     * 保存项目数据
     *
     * @return String
     */
    @RequestMapping(value = "/saveXM", method = RequestMethod.POST)
    public String saveXMHandler(@RequestParam int addflag, @RequestParam String xmdm, @RequestParam String xmmc, @RequestParam String xmms) {

        JSONObject res = new JSONObject();

        if(addflag == 1) {  //添加
            boolean ifExist = xmglService.queryIfXmExistService(xmdm);
            if (ifExist) {
                res.put("code", 0);
                res.put("message", "对应项目代码已存在");
            } else {
                int code = xmglService.insertXminfoService(xmdm, xmmc, xmms);
                res.put("code", code);
            }
        } else if(addflag == 0){    //编辑
            int code = xmglService.updateXminfoService(xmdm, xmmc, xmms);
            res.put("code", code);
        }

        return res.toString();
    }

    /**
     * 删除项目数据
     *
     * @return String
     */
    @RequestMapping(value = "/delXM", method = RequestMethod.POST)
    public String delXMHandler(@RequestParam String xmdm) {
        JSONObject res = new JSONObject();
        int code = xmglService.delXminfoService(xmdm);
        res.put("code", code);
        return res.toString();
    }

    /**
     * 删除项目数据
     *
     * @return String
     */
    @RequestMapping(value = "/getNewXmdm", method = RequestMethod.GET)
    public String getNewXmdmHandler() {
        JSONObject res = new JSONObject();
        res.put("xmdm", xmglService.getNewXmdmService());
        return res.toString();
    }

    /**
     * 查询项目数据集标准数据
     *
     * @return String
     */
    @RequestMapping(value = "/querySjjTable", method = RequestMethod.POST)
    public String querySjjTableHandler(@RequestParam String searchVal, @RequestParam String xmdm, @RequestParam String fldm,
                                       @RequestParam int pageNumber, @RequestParam int pageSize) {
        int startNum = (pageNumber-1)*pageSize;
        int endNum = pageNumber*pageSize;
        List<XmglEntity> sjjTableDataEntity = xmglService.querySjjTableService(searchVal, xmdm, fldm, startNum, endNum);
        if(sjjTableDataEntity.size() > 0){
            JSONObject res = new JSONObject();
            res.put("total", sjjTableDataEntity.size());
            res.put("rows", JSONUtils.toJSONArray(sjjTableDataEntity));
            return res.toString();
        }
        return null;
    }

    /**
     * 查询数据集分类树数据
     *
     * @return String
     */
    @RequestMapping(value = "/querySjjflTree", method = RequestMethod.POST)
    public ResponseEntity querySjjflTreeHandler(@RequestParam String xmdm) {
        List<Object> sjjflTreeList = xmglService.querySjjflTreeService(xmdm);
        return ResponseEntity.ok().put("sjjflTreeList", sjjflTreeList);
    }

    /**
     * 查询导入数据集标准数据
     *
     * @return String
     */
    @RequestMapping(value = "/querySjjImportTable", method = RequestMethod.POST)
    public String querySjjImportTableHandler(@RequestParam String searchVal, @RequestParam String xmdm,
                                             @RequestParam int pageNumber, @RequestParam int pageSize) {
        int startNum = (pageNumber-1)*pageSize;
        int endNum = pageNumber*pageSize;
        List<XmglEntity> sjjImportTableDataEntity = xmglService.querySjjImportTableService(searchVal, xmdm, startNum, endNum);
        if(sjjImportTableDataEntity.size() > 0){
            JSONObject res = new JSONObject();
            res.put("total", sjjImportTableDataEntity.size());
            res.put("rows", JSONUtils.toJSONArray(sjjImportTableDataEntity));
            return res.toString();
        }
        return null;
    }

    /**
     * 导入数据集
     *
     * @return ResponseEntity
     */
    @RequestMapping(value = "/importSjj", method = RequestMethod.POST)
    public ResponseEntity importSjjHandler(@RequestParam String importJson) {
        JSONArray jsonArray = JSONArray.parseArray(importJson);
        List<XmglEntity> importList = new LinkedList<>();
        for(int i=0; i<jsonArray.size(); i++){
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            XmglEntity entity = new XmglEntity();
            entity.setSjjdm(jsonObj.getString("sjjdm"));
            entity.setBbh(jsonObj.getString("bbh"));
            entity.setXmdm(jsonObj.getString("xmdm"));
            importList.add(entity);
        }
        int num = xmglService.importSjjService(importList);
        return ResponseEntity.ok().put("num", num);
    }

    /**
     * 删除数据集
     *
     * @return ResponseEntity
     */
    @RequestMapping(value = "/delSjj", method = RequestMethod.POST)
    public ResponseEntity delSjjHandler(@RequestParam String lsh) {
        int num = xmglService.delSjjService(lsh);
        return ResponseEntity.ok();
    }

    /**
     * 更新项目数据集版本
     *
     * @return ResponseEntity
     */
    @RequestMapping(value = "/updateVersion", method = RequestMethod.POST)
    public ResponseEntity updateVersionHandler(@RequestParam String xmdm) {
        int num = xmglService.updateVersionService(xmdm);
        return ResponseEntity.ok();
    }

    /**
     * 生成ETL脚本
     * @return
     */
    @RequestMapping(value = "/createEtlScript", method = RequestMethod.POST)
    public String createEtlScript(@RequestParam String xmdm) {
        return xmglService.createEtlScript(xmdm);
    }
}
